package com.spgf.model.rn;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.dao.LancamentoDAO;
import com.spgf.model.entities.Lancamento;
import com.spgf.model.enums.Mes;

public class LancamentoRN {

	private final Context	context;

	public LancamentoRN(final Context context) {
		this.context = context;
	}

	public void insert(final Lancamento pLancamento) {
		new LancamentoDAO(context).executeInsert(pLancamento);
	}

	public List<Lancamento> obterLancamentosPosterioresMes(Mes pMes, String pAno) throws NegocioException {

		Calendar lCalendarInicio = Calendar.getInstance();
		lCalendarInicio.set(Calendar.YEAR, Integer.valueOf(pAno));
		lCalendarInicio.set(Calendar.MONTH, pMes.ordinal());
		lCalendarInicio.set(Calendar.HOUR, 0);
		lCalendarInicio.set(Calendar.MINUTE, 0);
		lCalendarInicio.set(Calendar.SECOND, 0);
		lCalendarInicio.set(Calendar.MILLISECOND, 0);

		Calendar lCalendarAtual = Calendar.getInstance();
		lCalendarAtual.set(Calendar.HOUR, 0);
		lCalendarAtual.set(Calendar.MINUTE, 0);
		lCalendarAtual.set(Calendar.SECOND, 0);
		lCalendarAtual.set(Calendar.MILLISECOND, 0);

		if (lCalendarInicio.getTime().equals(lCalendarAtual.getTime())) {
			lCalendarInicio.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + 1);
		}

		else if (lCalendarInicio.after(lCalendarAtual)) {
			lCalendarInicio.set(Calendar.DAY_OF_MONTH, lCalendarInicio.getMinimum(Calendar.DAY_OF_MONTH));
		}

		else {
			lCalendarInicio.set(Calendar.DAY_OF_MONTH, lCalendarInicio.getMaximum(Calendar.DAY_OF_MONTH));
		}

		Calendar lCalendarFim = Calendar.getInstance();
		lCalendarFim.set(Calendar.YEAR, Integer.valueOf(pAno));
		lCalendarFim.set(Calendar.MONTH, pMes.ordinal());
		lCalendarFim.set(Calendar.DAY_OF_MONTH, lCalendarFim.getMaximum(Calendar.DAY_OF_MONTH));

		final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String sql = " " + LancamentoDAO.COLUNA_DATA + " BETWEEN '" + df.format(lCalendarInicio.getTime()) + "' AND '"
				+ df.format(lCalendarFim.getTime()) + "' ";

		return new LancamentoDAO(context).executeFindAll(new Lancamento(), null, sql);
	}

	@SuppressLint("SimpleDateFormat")
	public List<Lancamento> executarFindAll(Lancamento model) throws NegocioException {

		final Calendar calAtual = Calendar.getInstance();

		if (model.getData() != null) {
			calAtual.setTime(model.getData());
		}

		if (calAtual.get(Calendar.MONTH) != Calendar.getInstance().get(Calendar.MONTH)) {
			calAtual.set(Calendar.DATE, calAtual.getActualMaximum(Calendar.DATE));
		}

		final Calendar calInicial = Calendar.getInstance();
		calInicial.set(Calendar.DATE, 1);
		calInicial.set(Calendar.MONTH, calAtual.get(Calendar.MONTH));

		final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		// SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

		String sql = " " + LancamentoDAO.COLUNA_DATA + " BETWEEN '" + df.format(calInicial.getTime()) + "' AND '" + df.format(calAtual.getTime())
				+ "' ";

		if (model.getCategoria() != null && model.getCategoria().getCodigo() != null) {
			sql += " AND " + LancamentoDAO.COLUNA_CATEGORIA + " = " + model.getCategoria().getCodigo();
		}

		return new LancamentoDAO(context).executeFindAll(model, null, sql);

	}

}
